# -*- coding: utf_8 -*-

from plugin import PlugIn
from command_plugin import Cmd_Plugin
import random

try:
    from plugin_conf import fight_conf
except:
    print "Can't read the configure file: plugin_conf.py"
    print "maybe you should copy plugin_conf.py.default to plugin_conf.py"
    import sys
    sys.exit(1)

skills = ['重击','反击','连击']

class Cmd_Fight(Cmd_Plugin):
    def __init__(self):
        Cmd_Plugin.__init__(self)
        self.__users = {}
        
# ------------- Handlers below -------------

    def fightHandler(self, who, msg):
        '"/fight anyone" Fight against target player'
        if not self.__users.has_key(self._owner.getindexname(who)):
            self.buildFighter(who)
            self._owner.systoone(who, 'You are promoted to a fighter.')
    
    def rebornHandler(self, who, msg):
        '"/reborn" Reborn like the name of the command.'
        indexname = self._owner.getindexname(who)
        if not self.__users.has_key(indexname):
            self._owner.systoone(who, 'You are not a fighter! Try /fight first.')
            return
        self.buildFighter(who)
        self._owner.systoone(who, 'You are reborned.')

    def statusHandler(self, who, msg):
        '"/status" Show your status.'
        if not self.__users.has_key(self._owner.getindexname(who)):
            self._owner.systoone(who, 'You are not a fighter!')
            return
        self._owner.systoone(who, self.readDetail(who))

# ------------- Handlers above -------------

    def readSimple(self, who):
        indexname = self._owner.getindexname(who)
        info = self.__users[indexname]
        return ' '.join('%s: %s' % (key, info[key]) for key in info.keys())
        
    def readDetail(self, who):
        indexname = self._owner.getindexname(who)
        info = self.__users[indexname]
        return '\n'.join('%s: %s' % (key, info[key]) for key in info.keys() if key!='skill') + '\nskill: %s' % ','.join(info['skill'])
    
    def buildFighter(self, who):
        indexname = self._owner.getindexname(who)
        self.__users[indexname] = {
            'level': 1,
            'hp':20,
            'mp':1,
            'attack':5,
            'defense':1,
            'agile':1,
            'accurate':1,
            'luck':1,
            'skill':[random.choice(skills)]
            }
